package object;


public class MyLinkedList {
    static class Node {
        public Node prev = null;
        public Node next = null;
        public String value;

        public Node(String value) {
            this.value = value;
        }
    }
    //表示整个链表,同时记录尾巴
    private Node head = null;
    private Node tail = null;

    @Override
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Node cur = head; cur != null; cur = cur.next) {
            stringBuilder.append(cur.value);
            if(cur.next != null){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
    public int size(){
        int size = 0;
        for (Node cur = head; cur != null; cur = cur.next) {
            size++;
        }
        return size;
    }

    public void addFirst(String value){
        Node newNode = new Node(value);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
    }

    public void addLast(String value){
        Node newNode = new Node(value);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.prev = tail;
            tail.next = newNode;
            newNode.next = null;
            tail = newNode;
        }

    }
    public void add(int index,String value){
        int size = size();
        if (index < 0 || index > size) {
            throw new RuntimeException("下标越界");
        }
        if (index == 0) {
            addFirst(value);
            return;
        }
        if (index == size) {
            addLast(value);
            return;
        }
        Node newNode = new Node(value);
        Node cur = head;
        for (int i = 0; i < index -1; i++) {
           cur = cur.next;
        }
        Node next = cur.next;
        newNode.prev = cur;
        cur.next = newNode;
        newNode.next = next;
        next.prev = newNode;
    }

    public void clear(){
        head = null;
        tail = null;
    }

    public boolean contains(String value){
        for (Node cur = head; cur != null; cur = cur.next) {
            if(cur.value.equals(value)){
                return true;
            }
        }
        return false;
    }

    public void removeFirst(){
        if (head == null) {
            return;
        }
        if(head.next == null){
            head = null;
            tail = null;
        }
        head = head.next;
        head.prev = null;
    }

    public void removeLast(){
        if (head == null) {
            return;
        }
        if(head.next == null){
            head = null;
            tail = null;
        }
        Node prev = tail.prev;
        prev.next = null;
        tail = prev;
    }

    public void remove(int index){
        if(index < 0 || index >= size()){
            throw new RuntimeException("下标越界");
        }
        if(index == 0){
            removeFirst();
            return;
        }
        if(index == size() - 1){
            removeLast();
            return;
        }
        Node prev = head;
        for (int i = 0; i < index - 1; i++) {
            prev = prev.next;
        }
        Node toRemove = prev.next;
        Node next = toRemove.next;
        prev.next = next;
        next.prev = prev;
    }

    public void remove(String value){
        if(head == null){
            return;
        }
        if(head.value.equals(value)){
            removeFirst();
            return;
        }
        if(tail.value.equals(value)){
            removeLast();
            return;
        }
        Node cur = head;
        for( ;cur != null; cur = cur.next){
            if (cur.value.equals(value)) {
               break;
            }
        }
        if(cur == null){
            return;
        }
        Node prev = cur.prev;
        Node next = cur.next;
        prev.next = next;
        next.prev = prev;

    }

    public int indexOf(String value){
        int index = 0;
        for (Node cur = head; cur != null; cur = cur.next) {
            if (cur.value.equals(value)) {
                return index;
            }
            index++;
        }
        return -1;
    }

    private static void test1(){
        MyLinkedList list = new MyLinkedList();
        list.addFirst("a");
        list.addFirst("b");
        list.addFirst("c");
        System.out.println(list);
    }

    private static void test2(){
        MyLinkedList list = new MyLinkedList();
        list.addLast("a");
        list.addLast("b");
        list.addLast("c");
        list.addLast("d");
        System.out.println(list);
    }

    private static void test3(){
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.add(0,"a");
        myLinkedList.add(1,"b");
        myLinkedList.add(2,"c");
        myLinkedList.add(3,"d");
        System.out.println(myLinkedList);

    }

    public static void test4(){
        MyLinkedList list = new MyLinkedList();
        list.addLast("a");
        list.addLast("b");
        list.addLast("c");
        list.addLast("d");
        System.out.println(list.contains("a"));
        System.out.println( list.contains("x"));
        list.clear();
        System.out.println(list);

    }

    public static void test5(){
        MyLinkedList list = new MyLinkedList();
        list.addLast("a");
        list.addLast("b");
        list.addLast("c");
        list.addLast("d");
//        list.removeFirst();
//        System.out.println(list);
//        list.removeLast();
//        System.out.println(list);
        list.remove(1);
        System.out.println(list);
        list.remove("a");
        System.out.println(list);
    }



    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        //test4();
        test5();

    }
}
